//
//  TRCTSetViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/2/27.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TRCTSetViewController: BaseTRTCViewController {
    
    var selectIndex:Int = 0

    let dataList:[KIconGroup] = [
        KIconGroup.init(title: "常规", image: "icon_setting_bacis"),
        KIconGroup.init(title: "视频", image: "icon_setting_video"),
        KIconGroup.init(title: "音频", image: "icon_setting_audiok"),
        KIconGroup.init(title: "网络", image: "icon_setting_net")
    ]
    
    var vclActive:Bool = false
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kSetWinWidth, kSetWinHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.hk_refactoringWindow()
    }
    
    func hk_refactoringWindow() {
        self.view.window?.title = "设置"
        self.view.window?.styleMask = [.closable,.titled]
        self.view.window?.titlebarAppearsTransparent = true
        self.view.window?.titleVisibility = .hidden
        self.view.window?.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = true
        self.view.window?.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    lazy var container: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        return view
    }()
    
    func addChildrens(vcs:[NSViewController],selectIndex:Int?)  {
        for (inx,val) in vcs.enumerated() {
            self.addChild(val)
            if inx == selectIndex {
                self.container.addSubview(val.view)
                val.view.snp.makeConstraints { (make) in
                    make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
                }
            }
        }
    }
    
    func setupUI() {
        
        let ytop = 0//kNavBarHeight-kNavDefBarHeight
        
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.bottom.left.equalToSuperview()
            make.top.equalTo(ytop)
            make.width.equalTo(160)
        }
        
        self.view.addSubview(lineV)
        lineV.snp.makeConstraints { (make) in
            make.left.equalTo(scrollView.snp.right)
            make.top.equalTo(ytop)
            make.bottom.equalTo(scrollView)
            make.width.equalTo(1)
        }
        
        self.view.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.left.equalTo(scrollView.snp.right).offset(1)
            make.top.equalTo(ytop)
            make.right.bottom.equalToSuperview()
        }
        
        self.addChildrens(vcs: [
            BasicSetViewController(),
            VideoSetViewController(),
            AudioSetViewController(),
            NetSetViewController()
        ], selectIndex: 0)
        self.tabView.selectRowIndexes(NSIndexSet.init(index: 0) as IndexSet, byExtendingSelection: false)
    }
    
    func switchTo(vc:NSViewController) {
        for sView in self.container.subviews {
            sView.removeFromSuperview()
        }
        
        self.container.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
        }
    }
    
    lazy var lineV: NSView = {
        let view = NSView.init()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(240).cgColor
        return view
    }()
    
    
    lazy var scrollView: NSScrollView = {
        let view = NSScrollView.init(frame: NSRect.zero)
        view.wantsLayer = true
        view.backgroundColor = color_gray(250)
        view.documentView = self.tabView
        view.hasVerticalScroller = true
        view.autohidesScrollers = true
        view.automaticallyAdjustsContentInsets = false
        view.contentInsets = NSEdgeInsetsMake(20, 0, 0, 0)
        return view
    }()
    
    lazy var tabView: NSTableView = {
        let tab = NSTableView.init(frame: NSRect.zero)
        tab.wantsLayer = true
        tab.backgroundColor = color_gray(250)
        tab.delegate = self
        tab.dataSource = self
        tab.selectionHighlightStyle = .none
        
        let column = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier.init("column"))
        tab.addTableColumn(column)
        column.width = 160
        tab.headerView = nil
        return tab
    }()
}

extension TRCTSetViewController :NSTableViewDelegate ,NSTableViewDataSource {
    func numberOfRows(in tableView: NSTableView) -> Int {
        return self.dataList.count
    }
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier.init(mt_cell_default), owner: self) as? SetTabCellView
        if cell == nil {
            cell = SetTabCellView.init(group: self.dataList[row])
        }
        let isselect = self.selectIndex == row ? true:false
        cell?.isSelect = isselect
        return cell
    }
    
    func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        return 44
    }
    
    func tableViewSelectionDidChange(_ notification: Notification) {
        let tab = notification.object as? NSTableView
        let index = tab!.selectedRow
        if index >= 0 {
            self.switchTo(vc: self.children[index])
            
            self.selectIndex = index
            tab?.reloadData()
            
        }
       
    }
}
